/*
 * $Id: SongDownloadHandler.java 622 2008-09-02 07:35:54Z jasta00 $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.service;

import android.os.Handler;
import android.os.Message;

public abstract class SongDownloadHandler extends Handler
{
	public static final int MSG_SET_LENGTH = 0;
	public static final int MSG_ON_RECV = 1;
	public static final int MSG_FINISHED = 2;
	public static final int MSG_ERROR = 3;
	public static final int MSG_ABORTED = 4;
	public static final int MSG_BEGIN = 5;

	public abstract void onSetLength(long songId, long length);
	public abstract void onRecv(long songId, long recvd);
	public abstract void onFinished(long songId);
	public abstract void onError(long songId, String err);
	public abstract void onAborted(long songId);
	public abstract void onBegin(long songId);

	public void handleMessage(Message msg)
	{
		Long[] a;

		switch (msg.what)
		{
		case MSG_SET_LENGTH:
			a = (Long[])msg.obj;
			onSetLength(a[0], a[1]);
			break;
		case MSG_ON_RECV:
			a = (Long[])msg.obj;
			onRecv(a[0], a[1]);
			break;
		case MSG_FINISHED:
			onFinished((Long)msg.obj);
			break;
		case MSG_ERROR:
			Object[] o = (Object[])msg.obj;
			onError((Long)o[0], (String)o[1]);
			break;
		case MSG_ABORTED:
			onAborted((Long)msg.obj);
			break;
		case MSG_BEGIN:
			onBegin((Long)msg.obj);
			break;
		default:
			super.handleMessage(msg);
			break;
		}
	}

	public void sendSetLength(long songId, long length)
	{
		sendMessage(obtainMessage(MSG_SET_LENGTH,
		  new Long[] { songId, length }));
	}

	public void sendOnRecv(long songId, long recvd)
	{
		sendMessage(obtainMessage(MSG_ON_RECV,
		  new Long[] { songId, recvd }));
	}

	public void sendFinished(long songId)
	{
		sendMessage(obtainMessage(MSG_FINISHED, (Long)songId));
	}

	public void sendError(long songId, String err)
	{
		sendMessage(obtainMessage(MSG_ERROR,
		  new Object[] { (Long)songId, err }));
	}

	public void sendAborted(long songId)
	{
		sendMessage(obtainMessage(MSG_ABORTED, (Long)songId));
	}

	public void sendBegin(long songId)
	{
		sendMessage(obtainMessage(MSG_BEGIN, (Long)songId));
	}

	public static class DownloadStats
	{
		public long recvd = -1;
		public long size = -1;
	}
}
